﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace MonolithHelper
{
    public class MyConsole : TextWriter
    {
        RichTextBox _output = null;

        public MyConsole(RichTextBox output)
        {
            _output = output;
        }

        private static bool isTag;
        private static string style;

        public override void Write(char value)
        {


            base.Write(value);

            if (value == '<')
            {
                isTag = true;
                style = string.Empty;
                
            }
            else if (value == '>')
            {
                isTag = false;
                _output.SelectionStart = _output.Text.Length;
                _output.ScrollToCaret(); //Now scroll it automatically
                Application.DoEvents();

            }

            if (isTag == true && value != '<')
            {
                style = style + value;
            }


            if(!string.IsNullOrEmpty(style))
            {

                _output.DeselectAll();


                //size
                if (style == "H1")
                {
                    _output.SelectionFont = new Font(FontFamily.GenericSansSerif, 12);
                }
                else if (style == "H2")
                {
                    _output.SelectionFont = new Font(FontFamily.GenericSansSerif, 10);
                }


                //style

                if (style == "bold")
                {
                  _output.SelectionFont = new Font(_output.SelectionFont,FontStyle.Bold);
                }

                else if (style == "italic")
                {
                    _output.SelectionFont = new Font(_output.SelectionFont, FontStyle.Italic);

                }

                else if (style == "regular")
                {

                    _output.SelectionColor = Color.Black;



                    // _output.SelectionFont = new Font(_output.SelectionFont, FontStyle.Regular);

                    _output.SelectionFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular);


                }


               
                //color
                if (style == "blue")
                {
                    _output.SelectionColor = Color.Blue;  
                }
                else if (style == "green")
                {
                    _output.SelectionColor = Color.Green; 
                }
                else if (style == "red")
                {
                    _output.SelectionColor = Color.Red;
                }
                else if (style == "darkred")
                {
                    _output.SelectionColor = Color.DarkRed;
                }


             
            

            }


            if (isTag == false && value != '>')
            {

                //_output.Dispatcher.BeginInvoke(new Action(() =>
                //{
                _output.SelectionStart = _output.Text.Length;
                _output.AppendText(value.ToString());
               // _output.SelectionStart = _output.Text.Length; //Set the current caret position to the end
               

               

            }
            //})
            //); // When character data is written, append it to the text box. 
        }

        public override Encoding Encoding
        {
            get { return System.Text.Encoding.UTF8; }
        }

        public static void WriteLineRegular(string message)
        {
            Console.WriteLine("<bold>" + DateTime.Now.TimeOfDay.ToString() +  "<regular>   " + message);
        }

        public static void WriteLineColored(string messageBlue,string messageGreen)
        {
            Console.WriteLine("<bold>" + DateTime.Now.TimeOfDay.ToString() + "<regular>   <blue>" + messageBlue + " <green> --" + messageGreen + "<regular>");
        }

        public static void WriteLineStart(string message)
        {
            Console.WriteLine("<bold>" + DateTime.Now.TimeOfDay.ToString() + " <red><H1>****" + message + " ****START <regular>" );
        }

        public static void WriteLineEnd(string message)
        {
            Console.WriteLine("<bold>" + DateTime.Now.TimeOfDay.ToString() + " <red><H1>****" + message +" ****END <regular>");
        }

        public static void WriteLineError(string message)
        {
            Console.WriteLine("<bold>" + DateTime.Now.TimeOfDay.ToString() + " <darkred><H2>ERROR:  <italic>" +Environment.NewLine +  message + Environment.NewLine+  "      ***ERROR <regular>");
        }
       

    }

    public enum MyConsoleMode
    {
        WriteLineRegular=0,       
        WriteLineStart,
        WriteLineEnd,
        WriteLineError
    }

}
